import os.path
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


# 注册实例页面,3个表单的注册操作
class RegisterOperation:
    def __init__(self):
        self.driver = webdriver.Chrome()
        file = 'file:///' + os.path.abspath('html1/注册实例.html')
        self.driver.maximize_window()
        self.driver.get(file)

    # 第一个表注册 + 弹窗练习
    def register1(self):
        time.sleep(2)
        self.driver.find_element(By.ID, 'user').send_keys('abc')
        self.driver.find_element(By.ID, 'password').send_keys('123')
        self.driver.find_element(By.ID, 'tel').send_keys('18827553053')
        self.driver.find_element(By.ID, 'email').send_keys('1130780712@qq.com')
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//form/div[@id="zc"]/fieldset/button[@type="submit"]').click()
        print('注册成功1')
        self.driver.find_element(By.ID, 'alert').click()
        time.sleep(3)

    # 第二个表注册 + 下拉练习
    def register2(self):
        # 定位元素
        time.sleep(2)
        frame1 = self.driver.find_element(By.ID, 'idframe1')
        self.driver.switch_to.frame(frame1)
        self.driver.find_element(By.ID, 'userA').send_keys('abc')
        self.driver.find_element(By.ID, 'passwordA').send_keys('123')
        self.driver.find_element(By.ID, 'telA').send_keys('18827553053')
        self.driver.find_element(By.ID, 'emailA').send_keys('1130780712@qq.com')
        time.sleep(2)

        # 普通方式下拉
        self.driver.find_element(By.XPATH, '//select[@id="selectA"]/option[2]').click()
        time.sleep(3)

        # 点击注册
        self.driver.find_element(By.XPATH, '//form/div[@id="zc"]/fieldset/button[@type="submitA"]').click()
        print('注册成功2')
        time.sleep(2)

    # 第3和表注册 + 下拉练习
    def register3(self):
        # 需要先回到主页面
        # 定位元素
        self.driver.switch_to.default_content()
        time.sleep(2)
        frame2 = self.driver.find_element(By.NAME, 'myframe2')
        self.driver.switch_to.frame(frame2)
        self.driver.find_element(By.ID, 'userB').send_keys('abc')
        self.driver.find_element(By.ID, 'passwordB').send_keys('123')
        self.driver.find_element(By.ID, 'telB').send_keys('18827553053')
        self.driver.find_element(By.ID, 'emailB').send_keys('1130780712@qq.com')
        time.sleep(2)

        # 类的方式下拉
        # 定位到搜索框
        s1 = self.driver.find_element(By.ID, 'selectB')
        # 创建Select对象,然后实例化
        select = Select(s1)
        # 通过索引选取
        select.select_by_index(1)
        time.sleep(3)
        # 通过value值选取
        select.select_by_value('gz')
        time.sleep(3)
        # 通过文本选取
        select.select_by_visible_text('重庆B')
        time.sleep(3)

        # 注册
        self.driver.find_element(By.XPATH, '//form/div[@id="zc"]/fieldset/button[@value="注册B"]').click()
        print('注册成功3')
        time.sleep(2)

    def quit(self):
        time.sleep(3)
        self.driver.quit()


if __name__ == '__main__':
    case = RegisterOperation()
    case.register1()
    case.register2()
    case.register3()
    case.quit()
